<template>
  <div>
    <div id="title">
      新增药品
      <el-button type="primary" round id="title-button">返回</el-button>
    </div>
    <el-form
      :model="medicineDetailsForm"
      :rules="rules"
      ref="medicineDetailsForm"
      label-width="100px"
      class="detailsForm"
    >
      <el-form-item label="药品名称" prop="name">
        <el-input v-model="medicineDetailsForm.name" width="500"></el-input>
      </el-form-item>
      <el-form-item label="列表图" prop="image">
        <el-upload
          class="avatar-uploader"
          action="/"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <i class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <span style="color: gray">建议尺寸120*90像素 ,大小不要超过1M</span>
      </el-form-item>
      <el-form-item label="是否推荐" prop="isRecommended">
        <el-radio-group v-model="medicineDetailsForm.isRecommended">
          <el-radio label="1">是</el-radio>
          <el-radio label="2">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="药品分类" prop="medicineCategory">
        <el-cascader
          v-model="value"
          :options="options"
          @change="handleChange"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="规格" prop="specification">
        <el-input
          v-model="medicineDetailsForm.specification"
          width="500"
        ></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="medicineDetailsForm.price" width="500"></el-input>
      </el-form-item>
      <el-form-item label="库存" prop="stock">
        <el-input v-model="medicineDetailsForm.stock" width="500"></el-input>
      </el-form-item>
      <el-form-item label="是否处方药" prop="isPrescription">
        <el-radio-group v-model="medicineDetailsForm.isPrescription">
          <el-radio label="1">是</el-radio>
          <el-radio label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="药企" prop="companyName">
        <el-input
          v-model="medicineDetailsForm.companyName"
          width="500"
        ></el-input>
      </el-form-item>
      <el-form-item label="上架" prop="isListed">
        <el-switch v-model="medicineDetailsForm.isListed"></el-switch>
      </el-form-item>
      <el-form-item label="详细介绍" prop="content">
        <quill-editor
          ref="myTextEditor"
          v-model="medicineDetailsForm.content"
          :options="editorOption"
          style="height: 300px"
        ></quill-editor>
      </el-form-item>
      <el-form-item id="buttons">
        <el-button type="primary" @click="submitForm('medicineDetailsForm')"
          >立即创建</el-button
        >
        <el-button @click="resetForm('medicineDetailsForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";

import { quillEditor } from "vue-quill-editor";

export default {
  data() {
    return {
      medicineDetailsForm: {
        name: "",
        file: "",
        isRecommended: "",
        specification: "",
        price: "",
        stock: "",
        isPrescription: "",
        companyName: "",
        isListed: false,
        content: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入药品名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        specification: [
          { required: true, message: "请填写规格", trigger: "blur" },
        ],
        price: [{ required: true, message: "请填写价格", trigger: "blur" }],
        stock: [{ required: true, message: "请填写库存", trigger: "blur" }],
        companyName: [
          { required: true, message: "请填写药企名", trigger: "blur" },
        ],
        medicineCategory: [
          { required: true, message: "请选择药品分类", trigger: "blur" },
        ],

        isRecommended: [
          { required: true, message: "请选择是否推荐", trigger: "change" },
        ],
        isPrescription: [
          { required: true, message: "请选择是否为处方药", trigger: "change" },
        ],
      },
      editorOption: {
        placeholder: "请填写药品详细介绍",
      },
      value: [],
      options: [
        {
          value: "1",
          label: "心血管系统用药",
          children: [
            {
              value: "4",
              label: "钙通道阻滞用药",
            },
            {
              value: "5",
              label: "慢性心功能不全",
            },
            {
              value: "6",
              label: "抗心律失常药",
            },
          ],
        },
        {
          value: "2",
          label: "呼吸系统用药",
          children: [
            {
              value: "7",
              label: "镇咳药",
            },
            {
              value: "8",
              label: "祛痰药",
            },
            {
              value: "9",
              label: "平喘药",
            }
          ],
        },
         {
          value: "3",
          label: "呼吸系统用药",
          children: [],
        },
      ],
    };
  },
  methods: {
    submitForm(formName) {
      this.$axios({
        method: 'post',
        data: this.medicineDetailsForm,
        dataType: "json"
      }).then((res) => {
        console.log(res)
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      this.medicineDetailsForm.file=file;
      return false;
    },
    onEditorChange({ editor, html, text }) {
      this.content = html;
    },
  },
  components: {
    quillEditor,
  },
};
</script>
<style>
#title {
  font-size: 30px;
  font-weight: bolder;
}
#title-button {
  margin-left: 500px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.detailsForm {
  margin-top: 10px;
  padding-top: 20px;
  height: 1500px;
  width: 1000px;
  border-color: gray;
  border-style: solid;
  border-width: 1px;
}
#buttons {
  margin-top: 150px;
}
</style>